package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.IContext;

import java.io.FileWriter;
import java.io.FilterWriter;
import java.io.IOException;
import java.util.Optional;

@Controller
@SuppressWarnings("all")
public class IndexController {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private TemplateEngine templateEngine;

    /**
     * 首页显示--渲染方式
     * @return
     */
    @GetMapping({"/","/index.html"})
    public String index(Model model){
        //获取三级分类数据
        Result result = productFeignClient.getBaseCategoryList();

        model.addAttribute("list",result.getData());

        return "index/index";
    }

    /**
     * 首页显示-nginx部署方式
     * @return
     */
    @GetMapping("/createIndex")
    @ResponseBody
    public Result ceareIndex(){




        //获取数据
        Result result = productFeignClient.getBaseCategoryList();
        //封装数据对象
        Context context=new Context();
      context.setVariable("list",result.getData());
      //创建流对象
        FileWriter writer= null;
        try {
            writer = new FileWriter("F:\\demo\\index.html");
        } catch (IOException e) {
            e.printStackTrace();
        }

        //生成页面
        templateEngine.process("index/index.html",context,writer);

        return Result.ok("页面生成成功。。。。");
    }



}
